﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Moving03.Administrator
{
    public partial class WebForm12 : System.Web.UI.Page
    {
          DataClasses1DataContext context = new DataClasses1DataContext();
        int stt = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated == false || User.Identity.IsAuthenticated == true && !User.IsInRole("Admin"))
            {
                Response.Redirect("../Default.aspx");
            }
            
            int mavc = int.Parse(Request.QueryString["mavc"]);
            var query = from ddcn in context.DiaDiemCapNhats
                        join dd in context.DuongDis on ddcn.Id_DDCapNHat equals dd.Id_DDCapNhat
                        where ddcn.Id_ThongTinVanChuyen == mavc
                        select dd;
            Panel1.Controls.Add(new LiteralControl("<table  border='0'  width='100%' cellpadding='0' cellspacing='1'>"));
            Panel1.Controls.Add(new LiteralControl("<tr bgcolor='#507DD2' style='color: #FFFFFF; font-size:14px; height:40px' align='center' >"));
            Panel1.Controls.Add(new LiteralControl("<th>STT</th>"));
            Panel1.Controls.Add(new LiteralControl("<th>Đã đến</th>"));
            Panel1.Controls.Add(new LiteralControl("<th>Mã Id</th>"));
            Panel1.Controls.Add(new LiteralControl("<th>Nơi tới</th>"));
            Panel1.Controls.Add(new LiteralControl("<th>Ngày tới</th>"));
            Panel1.Controls.Add(new LiteralControl("</tr>")); 
            foreach (var i in query)
            {
                var query2 = (from ddtg in context.DiaDiemTrungGians
                             where ddtg.Id_DDTrungGian == i.Id_DDTGian
                             select ddtg).Single();

                Panel1.Controls.Add(new LiteralControl("<tr style='height:30px'>"));
                Panel1.Controls.Add(new LiteralControl("<td>"+stt));
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("<td>"));
                CheckBox ckbox = new CheckBox();
                ckbox.ID = "ckbox_"+stt.ToString();
                if(i.DaToiChua==1)
                    ckbox.Checked=true;
                Panel1.Controls.Add(ckbox);
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("<td>"));
                Label lb = new Label();
                lb.ID = "lb_" + stt.ToString();
                lb.Text = i.Id_DuongDi.ToString();
                Panel1.Controls.Add(lb);
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("<td>" + query2.TenDiaDiem));
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("<td>"));
                TextBox txtbox = new TextBox();
                txtbox.ID = "txtbox_" + stt.ToString();
                txtbox.Text = i.NgayDen.ToString();
                Panel1.Controls.Add(txtbox);
                Calendar cldar = new Calendar();
                cldar.ID = "cldar_" + stt.ToString();
                cldar.SelectionChanged+=new EventHandler(cldar_SelectionChanged);
                cldar.Visible = false;
                Panel1.Controls.Add(cldar);
                ImageButton igm = new ImageButton();
                igm.ImageUrl = "~/images/alarm.png";
                igm.ID = "igm_" + stt.ToString();
                igm.Click+=new ImageClickEventHandler(igm_Click);
                Panel1.Controls.Add(igm);
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("</tr>"));
                stt++;
                 
            }

            Panel1.Controls.Add(new LiteralControl("</table>"));
            Button capnhat = new Button();
            capnhat.Text = "Cập Nhật";
            capnhat.Click+=new EventHandler(capnhat_Click);
            Panel1.Controls.Add(capnhat);
            Button trove = new Button();
            trove.Text = "Trở về";
            trove.Click += new EventHandler(Button1_Click);
            Panel1.Controls.Add(trove);
            
        }
       
        private void igm_Click(object sender, EventArgs e)
        {
            ImageButton igm = new ImageButton();
            igm = (ImageButton)sender;
            string name =igm.ID.ToString();
            string[] arr_split= name.Split('_');
            Calendar time = new Calendar();
            string duoi = arr_split[1];
            string id= "cldar_" + duoi;
            time = Panel1.FindControl(id) as Calendar;
            time.Visible = !time.Visible;

 
        }
        public void cldar_SelectionChanged(object sender, EventArgs e)
        {
            DateTime datetime = new DateTime();
            Calendar calendar = (Calendar)sender;
            datetime = calendar.SelectedDate;
            string name = calendar.ID.ToString();
            string[] arr_split = name.Split('_');
            string duoi = arr_split[1];
            string id = "txtbox_" + duoi;
            TextBox txtbox = new TextBox();
            txtbox = Panel1.FindControl(id) as TextBox;
            txtbox.Text = datetime.ToString();
            calendar.Visible = false;

        }
        protected void capnhat_Click(object sender, EventArgs e)
        {
           for(int i = 1 ; i<stt-1; i++)
           {
               CheckBox ckbox = new CheckBox();
               string id_checkcbox = "ckbox_" + (i).ToString();
               ckbox = Panel1.FindControl(id_checkcbox) as CheckBox;
               if (ckbox.Checked == true)
               {
                   Label lb = new Label();
                   string id_label = "lb_" + (i).ToString();
                   lb = Panel1.FindControl(id_label) as Label;
                   int id = int.Parse(lb.Text);

                   
               
                   TextBox txtbox = new TextBox();
                   txtbox = Panel1.FindControl("txtbox_"+i.ToString()) as TextBox;
                   DateTime ngaykt = DateTime.Parse(txtbox.Text);
                   var query1 = (from duongdi in context.DuongDis
                                 where duongdi.Id_DuongDi == id
                                 select duongdi).Single();
                   query1.DaToiChua = 1;
                   query1.NgayDen = ngaykt;
                   context.SubmitChanges();
               }

           }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("CapNhatLH.aspx");
        }
    
    }
}